//package io.gitee.chearnee.web.config.feign;
//
//import io.gitee.chearnee.web.exception.ApiException;
//import io.gitee.chearnee.web.vo.ResultCode;
//import io.gitee.chearnee.web.vo.ResultVO;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import feign.Response;
//import feign.RetryableException;
//import feign.Util;
//import feign.codec.ErrorDecoder;
//import java.nio.charset.StandardCharsets;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//
///**
// * Feign Server抛出错误，直接返回给用户端，不经过FeignClient
// *
// * @author NN
// * @version 1.0
// * @date 2021/5/8 16:17
// */
//@Configuration
//public class FeignClientErrorDecoder extends ErrorDecoder.Default {
//
//    @Autowired
//    private ObjectMapper objectMapper;
//
//    @Override
//    public Exception decode(String methodKey, Response response) {
//        String errorContent;
//        try {
//            errorContent = Util.toString(response.body().asReader(StandardCharsets.UTF_8));
//            ResultVO resultVO = objectMapper.readValue(errorContent, ResultVO.class);
//            return new ApiException(resultVO.getMessage());
//        } catch (Exception e) {
//            Exception exception = super.decode(methodKey, response);
//            // 如果是RetryableException，则返回继续重试
//            if (exception instanceof RetryableException) {
//                return exception;
//            }
//            return new ApiException(ResultCode.SERVICEFAIL);
//        }
//    }
//}
